package com.iad.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;

import com.iad.core.Core;
import com.iad.core.Statics;

/**
 * Various static utility methods.
 * 
 * @author Nick Sophinos
 *
 */

public class Util {

	static String className = "Util";

	static Log log = Core.getLogger();


	/**
	 * Pad a string with spaces on the right.
	 *
	 * @param str String to add spaces
	 * @param width Width of string after padding
	 * @return String the padded string
	 */
	public static String padStringRight(String str, int width) {

		String logId = Util.getLogId(className, "padStringRight");
		log.debug(logId);

		StringBuffer buf = new StringBuffer(str);
		int space = width - buf.length();
		while (space-- > 0) {
			buf.append(' ');
		}
		return buf.toString();
	}

	/**
	 * A convenience method to forward a servlet to a different resource
	 * @param url the url to forward to
	 * @param request the servlet request object
	 * @param response the servlet request object
	 * @exception ServletException if there is a container problem
	 * @exception IOException if there is a problem reading the request or writing the response
	 */
	public static void forwardTo(String url, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String logId = Util.getLogId(className, "forwardTo");
		log.debug(logId);

		RequestDispatcher dispatcher = request.getRequestDispatcher(url);
		dispatcher.forward(request, response);
	}

	/**
	 * 
	 * @param className
	 * @param methodName
	 * @return
	 */
	public static String getLogId(String className, String methodName) {
		return (className + "." + methodName + "(): ");
	}

	/**
	 * 
	 * @param obj
	 * @param methodName
	 * @return
	 */
	public static String getLogId(Object obj, String methodName) {
		Class cls = obj.getClass();
		String className = cls.getName().substring(
				cls.getPackage().getName().length() + 1);
		return (className + ":" + methodName + "(): ");
	}

	
	/**
	 * 
	 * @param fname
	 * @param lname
	 * @return
	 */
	public static String createLoginFromFullName(String fname, String lname) {
		return fname.substring(0, 1).toLowerCase() + lname.toLowerCase();
	}

}
